package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.dhcc.DRGsHosp.application.DepGroupApplication;
import com.dhcc.DRGsHosp.core.domain.DepGroup;
import com.dhcc.DRGsHosp.core.domain.IndexCal;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;

@Named
@Transactional
public class DepGroupApplicationImpl implements DepGroupApplication {

	private QueryChannelService queryChannel;

	private EntityRepository repository;
	
	  @Autowired  
	  private JdbcTemplateObj jdbcTemplateObj; 
    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    
    private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}
    
	public DepGroup getDepGroup(String depGroupCode) {
		return DepGroup.get(DepGroup.class, depGroupCode);
	}
	
	public void creatDepGroup(DepGroup depGroup) {
		depGroup.save();
	}
	
	public void updateDepGroup(DepGroup depGroup) {
		depGroup .save();
	}
	
	public void removeDepGroup(DepGroup depGroup) {
		if(depGroup != null){
			depGroup.remove();
		}
	}
	
	public void removeDepGroups(Set<DepGroup> depGroups) {
		for (DepGroup depGroup : depGroups) {
			depGroup.remove();
		}
	}
	
	public List<DepGroup> findAllDepGroup() {
		return DepGroup.findAll(DepGroup.class);
	}
	/**
	 * 
	 *  @Description：获取科室基本信息
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.application.DepGroupApplication#findDepGroups(com.dhcc.DRGsHosp.core.domain.IndexCal)
	 */
	@Override
	public List<Map<String,Object>> findDepGroups(IndexCal queryVo){
		String dateFrom=DrgsIndex.formateDateFrom(queryVo.getDateFrom());
		String dateTo=DrgsIndex.formateDateTo(queryVo.getDateTo());
		String hospCode=queryVo.getHospCode();
		Object[] args=new Object[]{dateFrom,dateTo};
		String sql=" select depgroup.depgroup_code depGroupCode, depgroup.depgroup_desc depGroupDesc from DRGs.T_DEPGROUP depgroup where exists  "
				+ "(select 1 from DRGs.MR_IPINFO ipinfo left join drgs.mr_baseinfo mrb on ipinfo.pmainindex=mrb.pmainindex  left join DRGs.t_Department dep  on ipinfo.P00300001 = dep.dep_code  "
				+ " where to_char(ipinfo.P00290000,'yyyy-mm-dd') between ? and ? "
				+ "and dep.pardepgroup_code = depgroup.depgroup_code";
		    if(StringUtils.hasLength(hospCode)){
		  	  sql+= " and instr('"+hospCode+"',mrb.p00010001)>0   ";	
		    }
			  sql+= ")  order by depgroup.DEPGROUP_ORDER ";	
		return jdbcTemplateObj.queryForList(sql, args);
	}
	public List<DepGroup> findDepGroupList(IndexCal queryVo){
		String pType = "0";
		if(StringUtils.hasLength(queryVo.getpType())){
			pType = queryVo.getpType();
		}
		//先查归档表|再查检测表
		StringBuilder jpql = null;
		jpql = new StringBuilder("SELECT _depGroup from DepGroup _depGroup where exists(SELECT 1 from ArchiveData _archive where  _archive.adDepgroupcode=_depGroup.depGroupCode ");
		if(queryVo.getPardepCode()!=null && !"".equals(queryVo.getPardepCode()))//depcode
		{
			jpql.append(" and _archive.adDepcode ='"+queryVo.getPardepCode()+"'");
		}
		if(queryVo.getPargroupName()!=null && !"".equals(queryVo.getPargroupName()))//groupname--clinicname
		{
			jpql.append(" and _archive.adGroupname ='"+queryVo.getPargroupName()+"'");
		}
		if(queryVo.getParmdcCode()!=null && !"".equals(queryVo.getParmdcCode()))//mdcCode
		{
			jpql.append(" and _archive.adMdccode ='"+queryVo.getParmdcCode()+"'");
		}
		if(queryVo.getParadrgCode()!=null && !"".equals(queryVo.getParadrgCode()))//adrgCode
		{
			jpql.append(" and _archive.adAdrgcode ='"+queryVo.getParadrgCode()+"'");
		}
		if(queryVo.getPardrgCode()!=null && !"".equals(queryVo.getPardrgCode()))//drgCode
		{
			jpql.append(" and _archive.adDrgcode ='"+queryVo.getPardrgCode()+"'");
		}
		if(queryVo.getProtomedicus()!=null && !"".equals(queryVo.getProtomedicus()))
		{
			jpql.append(" and _archive.adDoctor ='"+queryVo.getProtomedicus()+"'");
		}
		// 注释报错
//		if(queryVo.getMrLot()!=null && !"".equals(queryVo.getMrLot()))//drgCode
//		{
//			jpql.append(" and _archive.adLot ='"+queryVo.getMrLot()+"'");
//		}
		if(queryVo.getPardepgroupCode()!=null && !"".equals(queryVo.getPardepgroupCode()) && !"DRG_DHCC".equals(queryVo.getPardepgroupCode()))//drgCode
		{
			jpql.append(" and _archive.adDepgroupcode ='"+queryVo.getPardepgroupCode()+"'");
		}
		jpql.append(" ) order by _depGroup.depGroupOrder ");
		List<DepGroup> mrList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
		if(mrList.size()<=0)
		{
			jpql.delete(0, jpql.length());
			jpql.append("select _depGroup from DepGroup _depGroup where exists(select 1 from IndexCal _IndexCal where  _IndexCal.pardepgroupCode=_depGroup.depGroupCode ");
			if (queryVo.getPardepCode() != null&& !"".equals(queryVo.getPardepCode())) {
				jpql.append(" and _IndexCal.pardepCode ='"+queryVo.getPardepCode()+"'");
			}
			if (queryVo.getPardrgCode() != null&& !"".equals(queryVo.getPardrgCode())) {
				jpql.append(" and _IndexCal.pardrgCode ='"+queryVo.getPardrgCode()+"'");
			}
			if (queryVo.getParmdcCode() != null&& !"".equals(queryVo.getParmdcCode())) {
				jpql.append(" and _IndexCal.parmdcCode ='"+queryVo.getParmdcCode()+"'");
			}
			if (queryVo.getParadrgCode() != null&& !"".equals(queryVo.getParadrgCode())) {
				jpql.append(" and _IndexCal.paradrgCode ='"+queryVo.getParadrgCode()+"'");
			}
			if (queryVo.getPargroupName() != null&& !"".equals(queryVo.getPargroupName())) {
				jpql.append(" and _IndexCal.pargroupName ='"+queryVo.getPargroupName()+"'");
			}
			if(queryVo.getProtomedicus()!=null && !"".equals(queryVo.getProtomedicus()))
			{
				jpql.append(" and _IndexCal.protomedicus ='"+queryVo.getProtomedicus()+"'");
			}
			// 注释报错
//			if(queryVo.getMrLot()!=null && !"".equals(queryVo.getMrLot()))//drgCode
//			{
//				jpql.append(" and _IndexCal.mrLot ='"+queryVo.getMrLot()+"'");
//			}
			jpql.append(" and _IndexCal.pType ='"+pType+"'");
			if(queryVo.getStdYear()!=null && !"".equals(queryVo.getStdYear()))//drgCode
			{
//				if("month".equals(queryVo.getMrStdtype())){
//		   			jpql.append(" and _IndexCal.mrStdtype = 'period'");
//		   		}else{
		   			jpql.append(" and _IndexCal.mrStdtype = 'year'");
//		   		}
			}
			if(queryVo.getPardepgroupCode()!=null && !"".equals(queryVo.getPardepgroupCode()) && !"DRG_DHCC".equals(queryVo.getPardepgroupCode()))//drgCode
			{
				jpql.append(" and _IndexCal.pardepgroupCode ='"+queryVo.getPardepgroupCode()+"'");
			}
			jpql.append(" )  order by _depGroup.depGroupOrder");
			mrList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
		}
		return mrList;
		
	}
	
	
	/**
     * 
     * @methodname    :isOrderExist
     * @description   : 查询该order序号是否已存在
     * @param         :
     * @author        :fulianxia
     * @return        :
     * @date          :2015年7月1日
     */
	@Override
	public Boolean isOrderExist(String order,String code){
		String jpql = "select _depGroup from DepGroup _depGroup   where _depGroup.depGroupOrder="+order;
		if(""!=code && null!=code){
			jpql += " and _depGroup.depGroupCode <> '"+code+"' ";
		}
		List<DepGroup> depGroups = getQueryChannelService().createJpqlQuery(jpql).list();
		if(depGroups.size() > 0){
			return true;
		}
		return false;
	}

	@Override
	public Double findOrderMax() {
		String sql = "select max(t.depgroup_order) from drgs.T_DEPGROUP t" ;
		Double intg = jdbcTemplateObj.queryForObject(sql, Double.class) ;
		if(intg == null) {
			return 0.;
		} else {
			return intg ;
		}
	}
}
